package it.softphone.commons.service.scripting.rhino;


import it.softphone.commons.json.server.JSONConverter;
import it.softphone.commons.json.server.JSONObject;
import it.softphone.commons.service.LogAware;

import java.util.Map;

import org.mozilla.javascript.NativeObject;

/**
 * 
 * @author bsorrentino
 *
 */
public class NativeToJSONObjectConverter implements JSONConverter<NativeObject>, LogAware{

	@Override
	public Object convert(@SuppressWarnings("rawtypes") Class type, Object value) {
		if( JSONObject.class.isAssignableFrom(type) && value instanceof NativeObject) {
			return this.convert((NativeObject) value);
		}
		return null;
	}

	@Override
	public Object convert(NativeObject value) {
		it.softphone.commons.json.server.JSONObject json = new it.softphone.commons.json.server.JSONObject();
		
		for( Map.Entry<Object,Object> e : value.entrySet() ) {
			json.put(String.valueOf(e.getKey()), e.getValue());
		}
		
		return json;
	}

}
